﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.BLL.UserProfileManagement;
using MS.InformationManagement.Core.TransactionsResponse;

namespace MS.InformationManagement.Presenter.UserProfileCreation
{
    public class UserProfileCreationPresenter : BasePresenter<IUserProfileCreationView>
    {
        public UserProfileCreationPresenter(IUserProfileCreationView view) : base(view) { }

        public void HandleLoad()
        {
            View.AvailableRoles = UserProfileCreationBLL.GetAllRoles();
            View.SelectedRoles = new List<DAL.DocumentCenter.Roles>();
            View.ProfileName = string.Empty;
            View.Profiles = UserProfileCreationBLL.GetProfiles();

        }

        public void SelectAllRoles()
        {
            if (View.AvailableRoles.Count > 0)
            {
                View.SelectedRoles = View.SelectedRoles == null ? (from role in View.AvailableRoles orderby role.Role select role).OrderBy(role=>role.Role).ToList()
                    : View.SelectedRoles.Union((from role in View.AvailableRoles select role)).OrderBy(role => role.Role).ToList();
                View.AvailableRoles = new List<DAL.DocumentCenter.Roles>();
            }
        }

        public void FromAvailableToSelected() {
            if (View.AvailableSelectedRoles.Count > 0) {
                View.SelectedRoles = View.SelectedRoles == null ? View.AvailableSelectedRoles.OrderBy(role=>role.Role).ToList()
                    : View.SelectedRoles.Union(View.AvailableSelectedRoles).OrderBy(role=>role.Role).ToList();
                List<MS.InformationManagement.DAL.DocumentCenter.Roles> tempRoles = View.AvailableRoles;
                View.AvailableSelectedRoles.ForEach(delegate (MS.InformationManagement.DAL.DocumentCenter.Roles role){
                    tempRoles.Remove(role);
                });
                View.AvailableRoles = tempRoles;
            }
        }

        public void UnSelectAllRoles()
        {
            if (View.SelectedRoles.Count > 0)
            {
                View.AvailableRoles = View.AvailableRoles == null ? (from role in View.SelectedRoles orderby role.Role select role).OrderBy(role => role.Role).ToList()
                    : View.AvailableRoles.Union((from role in View.SelectedRoles select role)).OrderBy(role => role.Role).ToList();
                View.SelectedRoles = new List<DAL.DocumentCenter.Roles>();
            }
        }

        public void FromSelectedToAvailable()
        {
            if (View.SelectedSelectedRoles.Count > 0)
            {
                View.AvailableRoles = View.AvailableRoles == null ? View.SelectedSelectedRoles.OrderBy(role => role.Role).ToList()
                    : View.AvailableRoles.Union(View.SelectedSelectedRoles).OrderBy(role=>role.Role).ToList();
                List<MS.InformationManagement.DAL.DocumentCenter.Roles> tempRoles = View.SelectedRoles;
                View.SelectedSelectedRoles.ForEach(delegate(MS.InformationManagement.DAL.DocumentCenter.Roles role)
                {
                    tempRoles.Remove(role);
                });
                View.SelectedRoles = tempRoles;
            }
        }

        public void CreateNewProfile()
        {
            //UserProfileCreationBLL.AddNewProfile(View.ProfileName, View.SelectedRoles, View.IsSiteContributor);
            CreateUserProfileCommand createUserProfileCommand = new CreateUserProfileCommand(View.ProfileName, View.SelectedRoles, View.IsSiteContributor);
            CreateUserProfileCommandResponse _createUserProfileCommandResponse = createUserProfileCommand.Execute() as CreateUserProfileCommandResponse;
            //View = _shippingResponse;
            //View.ShowMessage();


        }

        public void DeleteProfile()
        {
            //UserProfileCreationBLL.DeleteProfile(View.ProfileID);
            DeleteUserProfileCommand deleteUserProfileCommand = new DeleteUserProfileCommand(View.ProfileID);
            DeleteUserProfileCommandResponse _deleteUserProfileCommandResponse = deleteUserProfileCommand.Execute() as DeleteUserProfileCommandResponse;
            View.ProfileID = 0;
            View.Profiles = UserProfileCreationBLL.GetProfiles();
        }

        public void LoadProfile() {
            DAL.DocumentCenter.VincularPerfiles loadedProfile = UserProfileCreationBLL.GetProfile(View.ProfileID);
            if (loadedProfile != default(DAL.DocumentCenter.VincularPerfiles))
            {
                View.UpgradeProfileName = loadedProfile.Perfil;
                View.UpgradeSelectedRoles = loadedProfile.TipoRole.ToList();
                View.IsSiteContributorEdit = !loadedProfile.ContribuyenteEnSitio.HasValue ? false : loadedProfile.ContribuyenteEnSitio.Value;
                View.UpgradeAvailableRoles = UserProfileCreationBLL.GetAllRoles().Except(View.UpgradeSelectedRoles, new RoleEqualityComparer()).ToList();
            }
            else { throw new Exception("El perfil que esta intentando actualizar no existe"); }
        }

        public void UpgradeProfile() 
        {
            //UserProfileCreationBLL.UpdateProfile(View.ProfileID, View.UpgradeProfileName, View.UpgradeSelectedRoles, View.IsSiteContributorEdit);
            UpdateUserProfileCommand updateUserProfileCommand = new UpdateUserProfileCommand(View.ProfileID, View.UpgradeProfileName, View.UpgradeSelectedRoles, View.IsSiteContributorEdit);
            UpdateUserProfileCommandResponse _updateUserProfileCommandResponse = updateUserProfileCommand.Execute() as UpdateUserProfileCommandResponse;
            View.ProfileID = 0;
            View.Profiles = UserProfileCreationBLL.GetProfiles();
        }

        public void SelectAllRolesUpgrade() {
            if (View.AvailableRoles.Count > 0)
            {
                View.UpgradeSelectedRoles = View.UpgradeSelectedRoles == null ? (from role in View.UpgradeAvailableRoles orderby role.Role select role).ToList()
                    : View.UpgradeSelectedRoles.Union((from role in View.UpgradeAvailableRoles select role)).OrderBy(role=>role.Role).ToList();
                View.UpgradeAvailableRoles = new List<DAL.DocumentCenter.Roles>();
            }
        }

        public void FromAvailableToSelectedUpgrade()
        {
            if (View.UpgradeAvailableSelectedRoles.Count > 0)
            {
                View.UpgradeSelectedRoles = View.UpgradeSelectedRoles == null ? View.UpgradeAvailableSelectedRoles.OrderBy(role=>role.Role).ToList()
                    : View.UpgradeSelectedRoles.Union(View.UpgradeAvailableSelectedRoles).OrderBy(role=>role.Role).ToList();
                List<MS.InformationManagement.DAL.DocumentCenter.Roles> tempRoles = View.UpgradeAvailableRoles;
                View.UpgradeAvailableSelectedRoles.ForEach(delegate(MS.InformationManagement.DAL.DocumentCenter.Roles role)
                {
                    tempRoles.Remove(role);
                });
                View.UpgradeAvailableRoles = tempRoles;
            }
        }

        public void UnSelectAllRolesUpgrade()
        {
            if (View.UpgradeSelectedRoles.Count > 0)
            {
                View.UpgradeAvailableRoles = View.UpgradeAvailableRoles == null ? (from role in View.UpgradeSelectedRoles orderby role.Role select role).ToList()
                    : View.UpgradeAvailableRoles.Union((from role in View.UpgradeSelectedRoles select role)).OrderBy(role=>role.Role).ToList();
                View.UpgradeSelectedRoles = new List<DAL.DocumentCenter.Roles>();
            }
        }

        public void FromSelectedToAvailableUpgrade()
        {
            if (View.UpgradeSelectedSelectedRoles.Count > 0)
            {
                View.UpgradeAvailableRoles = View.UpgradeAvailableRoles == null ? View.UpgradeSelectedSelectedRoles.OrderBy(role=> role.Role).ToList()
                    : View.UpgradeAvailableRoles.Union(View.UpgradeSelectedSelectedRoles).OrderBy(role=>role.Role).ToList();
                List<MS.InformationManagement.DAL.DocumentCenter.Roles> tempRoles = View.UpgradeSelectedRoles;
                View.UpgradeSelectedSelectedRoles.ForEach(delegate(MS.InformationManagement.DAL.DocumentCenter.Roles role)
                {
                    tempRoles.Remove(role);
                });
                View.UpgradeSelectedRoles = tempRoles;
            }
        }
    }

    public class RoleEqualityComparer : IEqualityComparer<DAL.DocumentCenter.Roles>
    {

        public bool Equals(DAL.DocumentCenter.Roles x, DAL.DocumentCenter.Roles y)
        {
            return x.Id.Equals(y.Id);
        }

        public int GetHashCode(DAL.DocumentCenter.Roles obj)
        {
            return obj.Id.GetHashCode() * obj.Role.GetHashCode();
        }
    }
}
